using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_NewsletterSubscriptionEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        NewsletterSubscription newsletterSubscription;

        if (Int32.TryParse(Request["id"], out id))
            newsletterSubscription = NewsletterSubscription.Load(id);
        else
            newsletterSubscription = null;

        if (newsletterSubscription != null)
        {
            if (!string.IsNullOrEmpty(newsletterSubscription.SubscriberName))
				tbxSubscriberName.Text = newsletterSubscription.SubscriberName;
			if (!string.IsNullOrEmpty(newsletterSubscription.SubscriberEmail))
				tbxSubscriberEmail.Text = newsletterSubscription.SubscriberEmail;
			tbxNewsletterGroupId.Text = newsletterSubscription.NewsletterGroupId.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string subscriberName = Security.BindStringInput(tbxSubscriberName.Text, _securityLevel);
		string subscriberEmail = Security.BindStringInput(tbxSubscriberEmail.Text, _securityLevel);
		int newsletterGroupId = Security.BindIntegerInput(tbxNewsletterGroupId.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (NewsletterSubscription.Save(
				id, 
				subscriberName,
				subscriberEmail,
				newsletterGroupId
                ))
                    Response.Redirect("newslettersubscriptionlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            NewsletterSubscription newsletterSubscription = NewsletterSubscription.Create(
				subscriberName,
				subscriberEmail,
				newsletterGroupId
                );

            if (newsletterSubscription != null)
                Response.Redirect("newslettersubscriptionlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("newslettersubscriptionlist.aspx");
    }
}
